//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmHestonHullWhiteSolver.h"
using namespace Cephei::QL::Methods::Finitedifferences::Solvers;
#include <gen/QL/Processes/HestonProcess.h>
#include <gen/QL/Processes/HullWhiteProcess.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSolverDesc.h>
#include <gen/QL/Methods/Finitedifferences/Solvers/FdmSchemeDesc.h>
using namespace Cephei::QL::Processes;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::CFdmHestonHullWhiteSolver (Cephei::QL::Processes::IHestonProcess^ hestonProcess, Cephei::QL::Processes::IHullWhiteProcess^ hwProcess, Double corrEquityShortRate, Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSolverDesc^ solverDesc, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CHestonProcess^ _ChestonProcess;
    CHullWhiteProcess^ _ChwProcess;
    CFdmSolverDesc^ _CsolverDesc;
    CFdmSchemeDesc^ _CschemeDesc;
    try
    {
#ifdef HANDLE
        _phFdmHestonHullWhiteSolver = NULL;
#endif
        _ChestonProcess = safe_cast<CHestonProcess^> (hestonProcess);
        _ChestonProcess->Lock();
        Handle<QuantLib::HestonProcess>& _hestonProcess = static_cast<Handle<QuantLib::HestonProcess>&> (_ChestonProcess->GetHandle ()); 
        _ChwProcess = safe_cast<CHullWhiteProcess^> (hwProcess);
        _ChwProcess->Lock();
        Handle<QuantLib::HullWhiteProcess>& _hwProcess = static_cast<Handle<QuantLib::HullWhiteProcess>&> (_ChwProcess->GetHandle ()); 
        QuantLib::Rate _corrEquityShortRate = (QuantLib::Rate)ValueHelper::Convert (corrEquityShortRate); //d
        _CsolverDesc = safe_cast<CFdmSolverDesc^> (solverDesc);
        _CsolverDesc->Lock();
        QuantLib::FdmSolverDesc& _solverDesc = static_cast<QuantLib::FdmSolverDesc&> (_CsolverDesc->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc))
        {
            _CschemeDesc = safe_cast<CFdmSchemeDesc^> (schemeDesc->Value);
            _CschemeDesc->Lock();
        }
        QuantLib::FdmSchemeDesc& _schemeDesc = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>::IsSome::get (schemeDesc) ? static_cast<QuantLib::FdmSchemeDesc&> (_CschemeDesc->GetReference ()) : QuantLib::FdmSchemeDesc::Hundsdorfer()); //1
        _ppFdmHestonHullWhiteSolver = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteSolver> (new QuantLib::FdmHestonHullWhiteSolver ( _hestonProcess,  _hwProcess,  _corrEquityShortRate,  _solverDesc,  _schemeDesc ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_ChestonProcess != nullptr) _ChestonProcess->Unlock();
        if (_ChwProcess != nullptr) _ChwProcess->Unlock();
        if (_CsolverDesc != nullptr) _CsolverDesc->Unlock();
        if (_CschemeDesc != nullptr) _CschemeDesc->Unlock();
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::CFdmHestonHullWhiteSolver (boost::shared_ptr<QuantLib::FdmHestonHullWhiteSolver>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteSolver = NULL;
#endif
	_ppFdmHestonHullWhiteSolver = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::CFdmHestonHullWhiteSolver (QuantLib::FdmHestonHullWhiteSolver& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmHestonHullWhiteSolver = NULL;
#endif
	_ppFdmHestonHullWhiteSolver = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteSolver> (&childNative);
    
    _FdmHestonHullWhiteSolverOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::CFdmHestonHullWhiteSolver (CFdmHestonHullWhiteSolver^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteSolver = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmHestonHullWhiteSolver = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteSolver> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::CFdmHestonHullWhiteSolver (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteSolver = NULL;
#endif
	if (!t->IsSubclassOf(CFdmHestonHullWhiteSolver::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::CFdmHestonHullWhiteSolver (QuantLib::Handle<QuantLib::FdmHestonHullWhiteSolver>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmHestonHullWhiteSolver = &childNative;
	_ppFdmHestonHullWhiteSolver = &static_cast<boost::shared_ptr<QuantLib::FdmHestonHullWhiteSolver>>(childNative.currentLink());
    
    _FdmHestonHullWhiteSolverOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::CFdmHestonHullWhiteSolver (QuantLib::Handle<QuantLib::FdmHestonHullWhiteSolver> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmHestonHullWhiteSolver = &childNative;
	_ppFdmHestonHullWhiteSolver = &static_cast<boost::shared_ptr<QuantLib::FdmHestonHullWhiteSolver>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::CFdmHestonHullWhiteSolver (QuantLib::FdmHestonHullWhiteSolver childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmHestonHullWhiteSolver = NULL;
#endif
	_ppFdmHestonHullWhiteSolver = new boost::shared_ptr<QuantLib::FdmHestonHullWhiteSolver> (new QuantLib::FdmHestonHullWhiteSolver (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::~CFdmHestonHullWhiteSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmHestonHullWhiteSolver != NULL)
    {
	    delete _ppFdmHestonHullWhiteSolver;
        _ppFdmHestonHullWhiteSolver = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::!CFdmHestonHullWhiteSolver ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmHestonHullWhiteSolver != NULL)
    {
	    delete _ppFdmHestonHullWhiteSolver;
    }
}
QuantLib::FdmHestonHullWhiteSolver& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::GetReference ()
{
    if (_ppFdmHestonHullWhiteSolver == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmHestonHullWhiteSolver;
}
boost::shared_ptr<QuantLib::FdmHestonHullWhiteSolver>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::GetShared ()
{
    if (_ppFdmHestonHullWhiteSolver == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmHestonHullWhiteSolver;
}
QuantLib::FdmHestonHullWhiteSolver* Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::GetPointer ()
{
    if (_ppFdmHestonHullWhiteSolver == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmHestonHullWhiteSolver;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmHestonHullWhiteSolver>& Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::GetHandle ()
{
	if (_phFdmHestonHullWhiteSolver == NULL)
	{
		_phFdmHestonHullWhiteSolver = new Handle<QuantLib::FdmHestonHullWhiteSolver> (*_ppFdmHestonHullWhiteSolver);
	}
	return *_phFdmHestonHullWhiteSolver;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::HasNative () 
{
	return (_ppFdmHestonHullWhiteSolver != NULL);
}

Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::DeltaAt (Double s, Double v, Double r, Double eps)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
        QuantLib::Real _v = (QuantLib::Real)ValueHelper::Convert (v); //a
        QuantLib::Rate _r = (QuantLib::Rate)ValueHelper::Convert (r); //a
        QuantLib::Real _eps = (QuantLib::Real)ValueHelper::Convert (eps); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmHestonHullWhiteSolver)->deltaAt ( _s,  _v,  _r,  _eps );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::GammaAt (Double s, Double v, Double r, Double eps)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
        QuantLib::Real _v = (QuantLib::Real)ValueHelper::Convert (v); //a
        QuantLib::Rate _r = (QuantLib::Rate)ValueHelper::Convert (r); //a
        QuantLib::Real _eps = (QuantLib::Real)ValueHelper::Convert (eps); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmHestonHullWhiteSolver)->gammaAt ( _s,  _v,  _r,  _eps );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::ThetaAt (Double s, Double v, Double r)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
        QuantLib::Real _v = (QuantLib::Real)ValueHelper::Convert (v); //a
        QuantLib::Rate _r = (QuantLib::Rate)ValueHelper::Convert (r); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmHestonHullWhiteSolver)->thetaAt ( _s,  _v,  _r );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver::ValueAt (Double s, Double v, Double r)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s); //a
        QuantLib::Real _v = (QuantLib::Real)ValueHelper::Convert (v); //a
        QuantLib::Rate _r = (QuantLib::Rate)ValueHelper::Convert (r); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmHestonHullWhiteSolver)->valueAt ( _s,  _v,  _r );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::Solvers::IFdmHestonHullWhiteSolver^ Cephei::QL::Methods::Finitedifferences::Solvers::CFdmHestonHullWhiteSolver_Factory::Create (Cephei::QL::Processes::IHestonProcess^ hestonProcess, Cephei::QL::Processes::IHullWhiteProcess^ hwProcess, Double corrEquityShortRate, Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSolverDesc^ solverDesc, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Methods::Finitedifferences::Solvers::IFdmSchemeDesc^>^ schemeDesc)
{
    return REFNEW CFdmHestonHullWhiteSolver ( hestonProcess,  hwProcess,  corrEquityShortRate,  solverDesc,  schemeDesc);
}
